package script.mainpage.items
{
	import eventUtil.EventCenter;
	
	import laya.events.Event;
	
	import model.ConstantModel;
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.Userdata;
	import model.goods.GoodsItemVo;
	
	import script.ViewManager;
	
	import ui.items.GoodsItemUI;
	
	public class HomeGoodsCell extends GoodsItemUI
	{
		private var goodsVo:GoodsItemVo;
		public function HomeGoodsCell()
		{
			super();
		}
		
		public function setData(data:GoodsItemVo):void
		{
			goodsVo = data;
			this.goodsPic.skin = goodsVo.goodsImg;
			
			this.rawPriceLbl.text = "¥" + goodsVo.rawPrice.toFixed(2);
			
			this.sellPriceLbl.text = "¥" + goodsVo.sellPrice.toFixed(2);
			
			this.cutline.width = this.rawPriceLbl.textField.textWidth + 10;
			
			
			this.goodsNameLbl.text = "1份" + goodsVo.unitAmount + (goodsVo.offWeight != "0"?"±" + goodsVo.offWeight:"") + ConstantModel.UNIT_NAME[goodsVo.unit] + "|" + goodsVo.goodsName;
			
			this.commentlbl.text = goodsVo.comment ;
			
			this.limitPrice.visible = goodsVo.limitDiscount == 1;
			
			this.buy.on(Event.CLICK,this,onSaveToCart);
		}
		
		private function onSaveToCart():void
		{
			if(!Userdata.instance.isLogin)
			{
				ViewManager.showAlert("请先登录");
				ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);
				
				return;
			}
			
			var postdata:String = "goodsId=" + goodsVo.id + "&count=1";
			
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.addGoodsToCart,this,onAddGoodsBack,postdata,"post");
		}
		
		private function onAddGoodsBack(data:Object):void
		{
			
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				Userdata.instance.selectUserCart(result.data.cartId);
				
				EventCenter.instance.event(EventCenter.UPDATE_USER_CART_NUM);
			}
		}
	}
}